﻿#if (UNITY_STANDALONE || UNITY_ANDROID || UNITY_IOS || UNITY_WSA || UNITY_WEBGL) && UNITY_EDITOR
using System;
using System.Text;
using System.Collections.Generic;
using Net.Helper;
using Net.Share;
using UnityEditor;
using UnityEngine;

public class RpcHookTools
{
    [MenuItem("GameDesigner/Network/GenerateRpcHook")]
    private static void GenerateRpcHook()
    {
        var builder = new StringBuilder();
        builder.AppendLine("using Net.Adapter;");
        builder.AppendLine();
        builder.AppendLine("public static class RpcHook");
        builder.AppendLine("{");
        builder.AppendLine("\tpublic RpcMethodPtr[] Rpcptrs = new[]");
        builder.AppendLine("\t{");
        var methods = AssemblyHelper.GetMethodAttributes(typeof(Rpc), "mscorlib", "System", "Unity");
        var methodNames = new HashSet<string>();
        foreach (var method in methods)
        {
            var parameterText = string.Empty;
            foreach (var parameter in method.GetParameters())
            {
                parameterText += parameter.ParameterType.ToString() + ",";
            }
            parameterText = parameterText.TrimEnd(',');
            if (string.IsNullOrEmpty(parameterText))
                continue;
            if (methodNames.Add(parameterText))
                builder.AppendLine($"\t\tnew RpcMethodPtr<{parameterText}>(),");
        }
        builder.AppendLine("\t}");
        builder.AppendLine("}");
        Debug.Log(builder.ToString());
    }

    [MenuItem("GameDesigner/Network/GenerateRpcHashs")]
    private static void GenerateRpcHashs()
    {
        var builder = new StringBuilder();
        builder.AppendLine("using System;");
        builder.AppendLine("using Net.Helper;");
        builder.AppendLine();
        builder.AppendLine("public class RpcHashCollect : IAssemblyTypeHashCollect");
        builder.AppendLine("{");
        builder.AppendLine("\tprivate static readonly RpcHashCollect Instance = new();");
        builder.AppendLine();
        builder.AppendLine("\tpublic Type[] Types => new Type[]");
        builder.AppendLine("\t{");
        var methods = AssemblyHelper.GetMethodAttributes(typeof(Rpc), "mscorlib", "System", "Unity");
        var parameterTypes = new HashSet<Type>();
        foreach (var method in methods)
        {
            foreach (var parameter in method.GetParameters())
                parameterTypes.Add(parameter.ParameterType);
        }
        foreach (var type in parameterTypes)
        {
            builder.AppendLine($"\t\ttypeof({AssemblyHelper.GetCodeTypeName(type.ToString())}),");
        }
        builder.AppendLine("\t};");
        builder.AppendLine("\tstatic RpcHashCollect() => Collect();");
        builder.AppendLine();
        builder.AppendLine("\tpublic static void Collect()");
        builder.AppendLine("\t{");
        builder.AppendLine("\t\tforeach (var type in Instance.Types)");
        builder.AppendLine("\t\t{");
        builder.AppendLine("\t\t\tAssemblyHelper.AddHashType(type);");
        builder.AppendLine("\t\t}");
        builder.AppendLine("\t}");
        builder.AppendLine("}");

        Debug.Log(builder.ToString());
    }
}
#endif